package com.company.project.web;
import com.company.project.core.LayUiTableJson;
import com.company.project.core.Page;
import com.company.project.core.Result;
import com.company.project.core.ResultGenerator;
import com.company.project.model.Job;
import com.company.project.model.SysUser;
import com.company.project.service.JobService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
* Created by CodeGenerator on 2020/04/21.
*/
@RestController
@RequestMapping("/job")
public class JobController {
    @Resource
    private JobService jobService;

    @RequestMapping("/page/manage")
    public ModelAndView manage(){
        ModelAndView modelAndView = new ModelAndView("/workspace/job/jobList.html");
        return modelAndView;
    }

    @GetMapping("/page/detail")
    public ModelAndView detail(){
        ModelAndView modelAndView = new ModelAndView("/workspace/job/jobAdd.html");
        return modelAndView;
    }

    @PostMapping("/add")
    public Result add(Job job) {
        SysUser userinfo = (SysUser) SecurityUtils.getSubject().getPrincipal();
        job.setCreator(userinfo.getId());
        job.setUpdateTime(new Date());
        job.setUpdater(userinfo.getId());
        job.setStatus(1);
        jobService.save(job);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestParam Integer id) {
        jobService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(Job job) {
        jobService.update(job);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        Job job = jobService.findById(id);
        return ResultGenerator.genSuccessResult(job);
    }

    @GetMapping("/list")
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<Job> list = jobService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @RequestMapping("queryJobList")
    public LayUiTableJson queryJobList(@RequestParam(defaultValue = "1") int page
            , @RequestParam(defaultValue = "10") int limit){
        Page pageInfo = new Page();
        pageInfo.setCurrentPage(page);
        pageInfo.setSize(limit);
        Result ResponseResult = jobService.queryJobList(pageInfo);
        LayUiTableJson layUiTableJson = new LayUiTableJson(0,null,pageInfo.getAllRow(),(List)ResponseResult.getData());
        return layUiTableJson;
    }

    @GetMapping("/changeStatus")
    public Result changeStatus(@RequestParam("id") Integer id,@RequestParam("status") Integer status) {
        jobService.changeStatus(id,status);
        return ResultGenerator.genSuccessResult();
    }


}
